package cn.itsource.order.controller;

import cn.itsource.dto.OrderSubmitDto;
import cn.itsource.order.service.IOrderProductService;
import cn.itsource.utils.AjaxResult;
import cn.itsource.vo.ProductCartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/orderproduct")
public class OrderProductController {
    @Autowired
    private IOrderProductService orderProductService;


    /**
     * 将商品加入到购物车
     *
     * @param productId
     * @param count
     * @return
     */
    @GetMapping("/putcart/{productId}/{count}")
    public AjaxResult putCart(@PathVariable("productId") Long productId, @PathVariable("count") Integer count, HttpServletRequest request) {
        //添加时，要获取当前用户的信息
        String token = request.getHeader("token");
        return orderProductService.putCart(productId, count, token);
    }

    @GetMapping
    public List<ProductCartVo> findCartsByUserId(HttpServletRequest request) {
        String token = request.getHeader("token");
        return orderProductService.findCartsByUserId(token);
    }

    @PostMapping("/submitorder")
    public AjaxResult submitOrder(@RequestBody OrderSubmitDto dto, HttpServletRequest request) {
        String token = request.getHeader("token");
        return orderProductService.submitOrder(dto, token);
    }


}
